package com.twu.biblioteca;
import java.util.ArrayList;

class BookSection {

    ArrayList<Book> allBooks = new ArrayList<Book>();

    protected ArrayList<Book> availableBooks(){
        ArrayList<Book> availableBooks = new ArrayList<Book>();
        for (Book book:allBooks){
            if (book.available)
                availableBooks.add(book);
        }
        return availableBooks;
    }

    protected int numberOfAvailableBooks(){
        return availableBooks().size();
    }

    protected void releaseBook(String title){
        if (bookIsValidForRelease(title)){
            availableBooks().remove(selectBook(title));
            (selectBook(title)).available = false;
        }
    }

    protected void acceptBook(String title){
        if (bookIsValidForReturn(title)){
            availableBooks().add(selectBook(title));
            (selectBook(title)).available = true;
        }
    }

    public void printAvailableBooks(){
        System.out.println("Available books: ");
        for (Book book: availableBooks() ){
            System.out.println(book.title);
        }
    }

    protected boolean bookIsValidForRelease(String title){
        for (Book book: availableBooks()){
            if (book.title.equals(title))
                return true;
        }
        return false;

    }

    protected boolean bookIsValidForReturn(String title){
        for (Book book: allBooks){
            if (book.title.equals(title) && !book.available){
                return true;
            }
        }
        return false;
    }

    public Book selectBook(String title){
        for (Book book :allBooks){
            if ((book.title).equals(title))
                return book;
        }
        return null;
    }

}